package com.qf.lja.servlet;


import com.qf.lja.dao.OrderDao;
import com.qf.lja.enity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(urlPatterns = "/payCon")
public class PayConfirmServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //utf-8
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        String pass = req.getParameter("payment_password");
        String orderId = req.getParameter("orderId");

        System.out.println("order : " + orderId);

        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");

        System.out.println("userpa : " + user.getUserPass());
        System.out.println("pass : " + pass);


        //支付成功
        if ("123456".equals(pass)) {
            //跟新状态
            OrderDao orderDao = new OrderDao();
            orderDao.updateOrderStatus(orderId);

            //修改库存
            orderDao.updateStock(orderId);
            req.getRequestDispatcher("/paysuccess.jsp").forward(req, resp);
        } else {
            req.setAttribute("orderId", orderId);
            req.setAttribute("msg", "支付密码错误");
            req.getRequestDispatcher("/payfail.jsp").forward(req, resp);
        }
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //utf-8
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        String pass = req.getParameter("payment_password");
        String orderId = req.getParameter("orderId");

        System.out.println("order : " + orderId);

        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");

        System.out.println("userpa : " + user.getUserPass());
        System.out.println("pass : " + pass);


        //支付成功
        if (user.getUserPass().equals(pass)) {
            //跟新状态
            OrderDao orderDao = new OrderDao();
            orderDao.updateOrderStatus(orderId);

            //修改库存
            orderDao.updateStock(orderId);
            req.getRequestDispatcher("/paysuccess.jsp").forward(req, resp);
        } else {
            req.setAttribute("orderId", orderId);
            req.setAttribute("msg", "支付密码错误");
            req.getRequestDispatcher("/payfail.jsp").forward(req, resp);
        }
    }
}
